<?php


namespace app\api\controller\mall;


use app\common\controller\AppCommon;
use app\service\DiyLog;
use app\service\GeTuiService;
use think\Controller;
use think\Request;

class Merchant extends ApiBase
{
    protected $secret = '';

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        //todo 后台配置
        $this->secret = get_random(16);
    }


    //绑定CID
    public function bind_cid()
    {
        if (!$this->app_key || !in_array($this->app_key, ['rwAk83UaoYcRWt4y'])) {
            data_return('app_key未授权', -1);
        }
        if (empty($this->param['token'])) {
            data_return('非法验证', 401);
        }
        if (empty($this->param['cid'])) {
            data_return('参数不完整', -1);
        }
        $isUnbind = !empty($this->param['isUnbind']);
        $uid = cache('api_merchant_' . trim($this->param['token']));
        if (empty($uid)) {
            data_return('登录已过期，请重新登录', 401);
        }
        $data = AppCommon::data_get('plugin_merchant', ['uid' => $uid], 'id,uid,status');
        if (empty($data)) {
            data_return('商家账号不存在', -1);
        } elseif ($data['status'] <> 1) {
            data_return('您的状态异常，请联系管理');
        }


        $isUpdate = false;
        $cid = trim($this->param['cid']);
        //用户类型，1-普通用户，2-商家，3-后台管理
        $logCid = AppCommon::data_get('getui_cid', ['uid' => $uid, 'uid_type' => 2]);

        //用户解绑cid,仅针对某个uid，不能针对cid
        if ($isUnbind) {
            if (!empty($logCid)) {
                AppCommon::data_del('getui_cid', ['uid' => $uid, 'uid_type' => 2]);
                data_return('解绑成功');
            }
            data_return('您已解绑，重新绑定后可正常推送');
        }

        if (!empty($logCid)) {
            AppCommon::data_update('getui_cid', ['id' => $logCid['id']], ['cid' => $cid, 'up_time' => time()]);
            $isUpdate = true;
        } else {
            AppCommon::data_add('getui_cid', ['cid' => $cid, 'up_time' => time(), 'uid' => $uid, 'uid_type' => 2]);
        }

        //测试推送
        GeTuiService::pushToSingleByCid($cid, [
            'title' => $isUpdate ? '已更新绑定' : '绑定成功' . '，消息可正常推送',
            'content' => '本通知仅作为测试用，您操作绑定按钮后推送！',
            'clickType' => 'none'
        ]);

        data_return($isUpdate ? '已更新绑定' : '绑定成功', 0);
    }

    //登录返回token
    public function login()
    {
        if (!bs_p('merchant')) {
            data_return_arr('未安装多商户', -1);
        }
        if (empty($this->param['account'])) {
            data_return('账号未设置', -1);
        } elseif (empty($this->param['pwd'])) {
            data_return('密码未输入', -1);
        }
        $data = AppCommon::data_get('plugin_merchant', ['account' => trim($this->param['account'])], 'id,uid,pwd,salt,pwd_err_count,up_time,loginCount');

        if (empty($data)) {
            data_return('账号不存在', -1);
        }
        if ($data['pwd_err_count'] >= 5 && ($data['up_time'] > time() - 1800)) {
            data_return('密码错误次数过多，请稍后重试', -1);
        }
        if (md5($this->param['pwd'] . $data['salt']) <> $data['pwd']) {
            AppCommon::data_update('plugin_merchant', ['id' => $data['id']], ['pwd_err_count' => $data['pwd_err_count'] + 1, 'up_time' => time()]);
            data_return('账号密码不匹配', -1);
        }
        //重生盐值加密
        $salt = get_random(10, false);
        $pwd = md5($this->param['pwd'] . $salt);
        AppCommon::data_update('plugin_merchant', ['id' => $data['id']],
            ['pwd_err_count' => 0, 'salt' => $salt, 'pwd' => $pwd, 'loginCount' => $data['loginCount'] + 1]);

        $loginData = [
            'uid' => $data['uid'],
            'add_time' => time(),
            'ip' => get_ip()
        ];
        AppCommon::data_add('plugin_merchant_login_log', $loginData);

        $token = md5($data['uid'] . $this->secret . time());
        //七天有效
        cache('api_merchant_' . $token, $data['uid'], 86400 * 7);
        $store_num = AppCommon::data_get_column('stores', ['merchant_uid' => $data['uid']], 'store_num');
        $urls = [
            //'store' => URL_WEB . 'mall/goods/pro_list.html?store_num=' . (!empty($store_num) ? $store_num : '123456'),
            'store' => 'https://mall.wei1.top/',
            'manager' => URL_WEB . 'plugin/merchant.manager/index.html'
        ];
        data_return('登录成功', 0, ['token' => $token, 'urls' => $urls]);
    }

    public function logout()
    {

    }

}